var app = getApp()
var tuiModel = require("../../model/tuiModel.js")
var util=require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tags: ['全部', '已入账', '冻结中', '已取消'],
    tagIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var index = options.index
    util.showLoading("加载中")
    tuiModel.tuiAward(res => {
      wx.hideLoading();
      if(res.status==1){
        util.failToast(res.msg)
        return;
      }
      this.setData({
        tagIndex: index || 0,
        list: res.data,
        awards: res.data,
      })
    })
  },
  switchTag(e) {
    this.setData({
      tagIndex: parseInt(e.currentTarget.id),
      awards: getAwards(this.data.list, parseInt(e.currentTarget.id))
    })
  }

})
//当前显示优惠券列表
function getAwards(list, index) {
  if (index == 0) return list
  else if (index == 1) return getAwards1(list)
  else if (index == 2) return getAwards2(list)
  else if (index == 3) return getAwards3(list)
}

//已入账
function getAwards1(list) {
  var awards = []
  for (var i = 0; i < list.length; i++) {
    if (list[i].status == 1) awards.push(list[i])
  }
  return awards
}

//冻结中
function getAwards2(list) {
  var awards = []
  for (var i = 0; i < list.length; i++) {
    if (list[i].status == 0) awards.push(list[i])
  }
  return awards
}
// 已取消
function getAwards3(list) {
  var awards = []
  for (var i = 0; i < list.length; i++) {
    if (list[i].status == 3) awards.push(list[i])
  }
  return awards
}